package tree;

import java.util.LinkedList;
import java.util.Queue;

import com.mj.printer.BinaryTreeInfo;

@SuppressWarnings("unchecked")
public class BinaryTree<E> implements BinaryTreeInfo {
	protected int size;
	protected Node<E> root;

	public int size() {
		return size;
	}

	public boolean isEmpty() {
		return this.size == 0;
	}

	public void clear() {
		root = null;
		size = 0;
	}

	public void preorder(Visitor<E> visitor) {
		if (visitor == null) {
			return;
		}
		preorder(root, visitor);
	}

	private void preorder(Node<E> node, Visitor<E> visitor) {
		if (node == null || visitor.stop) {
			return;
		}
		visitor.stop = visitor.visit(node.element);
		preorder(node.left, visitor);
		preorder(node.right, visitor);
	}

	/*
	 * 中序遍历
	 * 
	 */

	public void inorder(Visitor<E> visitor) {
		if (visitor == null) {
			return;
		}
		inorder(root, visitor);
	}

	private void inorder(Node<E> node, Visitor<E> visitor) {
		if (node == null || visitor.stop) {
			return;
		}
		inorder(node.left, visitor);
		if (visitor.stop) {
			return;
		}
		visitor.stop = visitor.visit(node.element);
		inorder(node.right, visitor);
	}

	/*
	 * 后序遍历
	 * 
	 */

	public void postorder(Visitor<E> visitor) {
		if (visitor == null) {
			return;
		}
		postorder(root, visitor);
	}

	private void postorder(Node<E> node, Visitor<E> visitor) {
		if (node == null || visitor.stop) {
			return;
		}
		postorder(node.left, visitor);
		postorder(node.right, visitor);
		if (visitor.stop) {
			return;
		}
		visitor.stop = visitor.visit(node.element);
	}

	/*
	 * 层序遍历
	 * 
	 */
	public void levelorder(Visitor<E> visitor) {
		if (visitor == null) {
			return;
		}
		levelorder(root, visitor);
	}

	public void levelorder(Node<E> node, Visitor<E> visitor) {
		if (node == null) {
			return;
		}
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(node);
		while (queue.size() > 0) {
			Node<E> node2 = queue.poll();
			if (visitor.stop) {
				return;
			}
			visitor.stop = visitor.visit(node2.element);
			if (node2.left != null) {
				queue.offer(node2.left);
			}
			if (node2.right != null) {
				queue.offer(node2.right);
			}
		}
	}

	public boolean isComplete(Visitor<E> visitor) {
		if (root == null) {
			return false;
		}
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(root);
		boolean isLeaf = false;
		while (queue.size() > 0) {
			Node<E> node = queue.poll();
			if (isLeaf && node.isLeaf()) {
				return false;
			}
			if (node.left != null) {
				queue.offer(node.left);
			} else {
				if (node.right != null) {
					return false;
				}
			}

			if (node.right != null) {
				queue.offer(node.right);
			} else {
				if (node.left == null) {
					isLeaf = true;
				}
			}

		}
		return true;
	}

	public int height() {
		return height(root);
	}

	private int height(Node<E> node) {
		if (node == null) {
			return 0;
		}
		return 1 + Math.max(height(node.left), height(node.right));
	}

	protected Node<E> createNode(E element, Node<E> parent) {
		return new Node<E>(element, parent);
	}

	protected Node<E> predecessor(Node<E> node) {
		if (node == null) {
			return null;
		}
		if (node.left != null) {
			Node<E> newNode = node.left;
			while (newNode.right != null) {
				newNode = newNode.right;
			}
			return newNode;
		}

		while (node.parent != null && node == node.parent.left) {
			node = node.parent;
		}
		return node.parent;

	}

	protected Node<E> successor(Node<E> node) {
		if (node == null) {
			return node;
		}

		if (node.right != null) {
			Node<E> newNode = node.right;
			while (newNode.left != null) {
				newNode = newNode.left;
			}
			return newNode;
		}
		if (node.right == null && node.parent != null) {
			while (node.parent != null && node == node.parent.right) {
				node = node.parent;
			}
			return node.parent;
		}
		return node;
	}

	public static abstract class Visitor<E> {
		boolean stop;

		abstract boolean visit(E element);
	}

	public void invertTree() {
		Queue<Node<E>> queue = new LinkedList<>();
		queue.offer(this.root);

		while (queue.size() > 0) {
			Node<E> node = queue.poll();
			Node<E> lefTreeNode = node.left;
			Node<E> righTreeNode = node.right;

			if (lefTreeNode != null) {
				queue.offer(lefTreeNode);
				node.right = lefTreeNode;
			} else {
				node.right = null;
			}

			if (righTreeNode != null) {
				queue.offer(righTreeNode);
				node.left = righTreeNode;
			} else {
				node.left = null;
			}

		}

	}

	protected static class Node<E> {
		E element;
		Node<E> left;
		Node<E> right;
		Node<E> parent;

		public Node(E element, Node<E> parent) {
			this.element = element;
			this.parent = parent;
		}

		public boolean isLeaf() {
			return left == null && right == null;
		}

		public boolean hasTwoChildren() {
			return left != null && right != null;
		}

		public boolean isLeftChild() {
			return parent != null && this == parent.left;
		}

		public boolean isRightChild() {
			return parent != null && this == parent.right;
		}

	}

	@Override
	public Object root() {
		return this.root;
	}

	@Override
	public Object left(Object node) {
		return ((Node<E>) node).left;
	}

	@Override
	public Object right(Object node) {
		return ((Node<E>) node).right;
	}

	@Override
	public Object string(Object node) {
//		return ((Node<E>) node).element;
		return node;
	}
}
